package com.qiguliuxing.dts.db.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiguliuxing.dts.db.dao.DtsCouponUserMapper;
import com.qiguliuxing.dts.db.domain.DtsCouponUser;
import com.qiguliuxing.dts.db.util.CouponUserConstant;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class DtsCouponUserService {
	@Resource
	private DtsCouponUserMapper couponUserMapper;

	public Integer countCoupon(Integer couponId) {
		QueryWrapper<DtsCouponUser> userQueryWrapper = new QueryWrapper<>();
		userQueryWrapper.lambda().eq(DtsCouponUser::getCouponId, couponId);
		userQueryWrapper.eq("deleted", false);
		return couponUserMapper.selectCount(userQueryWrapper).intValue();
	}

	public Integer countUserAndCoupon(Integer userId, Integer couponId) {
		QueryWrapper<DtsCouponUser> userQueryWrapper = new QueryWrapper<>();
		userQueryWrapper.lambda().eq(DtsCouponUser::getUserId, userId);
		userQueryWrapper.lambda().eq(DtsCouponUser::getCouponId, couponId);
		userQueryWrapper.eq("deleted", false);
		return couponUserMapper.selectCount(userQueryWrapper).intValue();
	}

	public void add(DtsCouponUser couponUser) {
		couponUser.setAddTime(LocalDateTime.now());
		couponUser.setUpdateTime(LocalDateTime.now());
		couponUserMapper.insert(couponUser);
	}

	public List<DtsCouponUser> queryList(Integer userId, Integer couponId, Short status, Integer page, Integer size,
			String sort, String order) {
		QueryWrapper<DtsCouponUser> userQueryWrapper = new QueryWrapper<>();
		if (userId != null) {
			userQueryWrapper.lambda().eq(DtsCouponUser::getUserId, userId);
		}
		if (couponId != null) {
			userQueryWrapper.lambda().eq(DtsCouponUser::getCouponId, couponId);
		}
		if (status != null) {
			userQueryWrapper.eq("status", status);
		}
		if (StrUtil.isNotEmpty(sort) && StrUtil.isNotEmpty(order)) {
			userQueryWrapper.orderByDesc(sort);
		}
		userQueryWrapper.eq("deleted", false);
		if (page!= null && size != null) {
			IPage<DtsCouponUser> iPage = new Page<>(page, size);
			return couponUserMapper.selectPage(iPage, userQueryWrapper).getRecords();
		}
		return couponUserMapper.selectList(userQueryWrapper);
	}

	public List<DtsCouponUser> queryAll(Integer userId, Integer couponId) {
		return queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
	}

	public List<DtsCouponUser> queryAll(Integer userId) {
		return queryList(userId, null, CouponUserConstant.STATUS_USABLE, null, null, "add_time", "desc");
	}

	public DtsCouponUser queryOne(Integer userId, Integer couponId) {
		List<DtsCouponUser> couponUserList = queryList(userId, couponId, CouponUserConstant.STATUS_USABLE, 1, 1,
				"add_time", "desc");
		if (couponUserList.size() == 0) {
			return null;
		}
		return couponUserList.get(0);
	}

	public DtsCouponUser findById(Integer id) {
		return couponUserMapper.selectById(id);
	}

	public int update(DtsCouponUser couponUser) {
		couponUser.setUpdateTime(LocalDateTime.now());
		return couponUserMapper.updateById(couponUser);
	}

	public List<DtsCouponUser> queryExpired() {
		QueryWrapper<DtsCouponUser> userQueryWrapper = new QueryWrapper<>();
		userQueryWrapper.eq("status", CouponUserConstant.STATUS_USABLE);
		userQueryWrapper.lambda().ge(DtsCouponUser::getEndTime, LocalDate.now());
		userQueryWrapper.eq("deleted", false);
		return couponUserMapper.selectList(userQueryWrapper);
	}
}
